<?php defined('SYSPATH') or die('No direct access allowed.');
/**
 * 新闻分类管理
 *
 * @author liuxudong
 */
class Controller_Admin_Newstype extends Controller_Admin_Based {
    /**
     * 添加分类
     */
    public function action_add() {
        $name_template = '<a href="/admin/newstype/edit/{id}" title="点击修改">{name}</a> <a href="/admin/newstype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $conditional_name_template = '<a href="/admin/newstype/edit/{id}" title="点击修改">[ {name} ]</a> <a href="/admin/newstype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $content_data['catalog_tree'] = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'cheers_main_newstypes', 'id' => 'chr_typeid', 'name' => 'chr_typename'));
        $content_data['product_ids'] = array();
        $content_data['is_conditional'] = FALSE;
        $catalog_opt_template = array(
            'name_box' => '
            <option value="{id}">{indentation}{name}</option>
            ',
            'indentation' => '&nbsp;|&nbsp;&nbsp;'
        );
        $content_data['catalog_opt'] = Catehelp::tree_to_str(Catehelp::get_tree($catalog_opt_template, 0, 0, array('table' => 'cheers_main_newstypes', 'id' => 'chr_typeid', 'name' => 'chr_typename')));

        if ($_POST) {
            $newstype = ORM::factory('cheers_main_newstype');
            $_POST['chr_createdate'] = time();
            $_POST['chr_editdate'] = time();
            $user = User::instance()->logged_in();
            $_POST['chr_creator'] = $user['chr_uid'];
            $_POST['chr_editor'] = $user['chr_uid'];
            $newstype->values($_POST);
            if ($newstype->check()) {
                $newstype->save();

                if ($newstype->saved()) {
                    $newstypefiles = ORM::factory('cheers_main_newsimage')
                            ->where('chr_flag', '=', 0)
                            ->find_all()
                            ->as_array();

                    if ($newstypefiles) {
                        DB::update('cheers_main_newsimages')
                                ->set(array('chr_imageid' => $newstype->chr_typeid, 'chr_filestype' => 1,'chr_flag'=>1))
                                ->where('chr_flag', '=', 0)
                                ->execute();
                    }
                    Message::set('新闻分类添加成功');
                    Request::current()->redirect('/admin/newstype/add/');
                }
            }
        }
        $content = View::factory('admin/news/newstype_add', $content_data)->render();
        $this->template = View::factory('admin/template')->set('content', $content);
    }

    /**
     * 删除分类
     */
    public function action_delete() {
        $chr_typeid = $this->request->param('id');
        $cheers_main_newstype = ORM::factory('cheers_main_newstype', $chr_typeid);
        if ($cheers_main_newstype->loaded()) {
            $parentids_array = Catehelp::get_childs($chr_typeid, 'cheers_main_newstypes', 'chr_typeid');
            $parentids = '"';
            foreach ($parentids_array as $key => $value) {

                $parentids .= $value . '","';
            }
            $parentids = rtrim(rtrim($parentids, '"'), ',');
            if (!empty($parentids)) {
                DB::query(Database::DELETE, "DELETE FROM cheers_main_newsimages WHERE chr_imageid in(" . $parentids . ")")->execute();
                DB::query(Database::DELETE, "DELETE FROM cheers_main_newstypes WHERE chr_typeid in(" . $parentids . ")")->execute();
                Message::set('分类删除成功!');
                Request::current()->redirect('/admin/newstype/add/');
            } else {
                $newstype_del = $cheers_main_newstype->delete();
                $newstypefiles = ORM::factory('cheers_main_newsimage', $chr_typeid)
                        ->where('chr_filestype', '=', 1)
                        ->find_all()
                        ->as_array();
                if ($newstypefiles) {
                    $newstypeiamges_del = ORM::factory('cheers_main_newsimage', $chr_typeid)
                            ->where('chr_filestype', '=', 1)
                            ->delete();
                    Message::set('分类删除成功!');
                    Request::current()->redirect('/admin/newstype/add/');
                } else {
                    Message::set('分类删除失败!', 'red');
                    Request::current()->redirect('/admin/newstype/add/');
                }
            }
        }
    }

    /**
     * 编辑分类
     */
    public function action_edit() {
        $name_template = '<a href="/admin/newstype/edit/{id}" title="点击修改">{name}</a> <a href="/admin/newstype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $conditional_name_template = '<a href="/admin/newstype/edit/{id}" title="点击修改">[ {name} ]</a> <a href="/admin/newstype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $content_data['catalog_tree'] = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'cheers_main_newstypes', 'id' => 'chr_typeid', 'name' => 'chr_typename'));
        $content_data['product_ids'] = array();
        $content_data['is_conditional'] = FALSE;
        $chr_typeid = $this->request->param('id');
        $catalog_opt_template = array(
            'name_box' => '<option value="{id}" {selected}>{indentation}{name}</option>',
            'indentation' => '&nbsp;|&nbsp;&nbsp;'
        );
        $content_data['catalog_opt'] = Catehelp::tree_to_str(Catehelp::get_tree($catalog_opt_template, 0, 0, array('table' => 'cheers_main_newstypes', 'id' => 'chr_typeid', 'name' => 'chr_typename'), $chr_typeid));

        $newstype = ORM::factory('cheers_main_newstype', $chr_typeid)->as_array();
        if ($_POST) {
            $newstype = ORM::factory('cheers_main_newstype', $chr_typeid);
            $data['chr_typename'] = htmlspecialchars(Arr::get($_POST, 'chr_typename', ''));
            $data['chr_parentid'] = Arr::get($_POST, 'chr_parentid', '');
            $data['chr_keywords'] = htmlspecialchars(Arr::get($_POST, 'chr_keywords', ''));
            $data['chr_desc'] = htmlspecialchars(Arr::get($_POST, 'chr_desc', ''));
            $data['chr_editdate'] = time();
            $user = User::instance()->logged_in();
            $data['chr_editor'] = $user['chr_uid'];
            $newstype->values($data);
            if ($newstype->check()) {
                $newstype->save();
                if ($newstype->saved()) {
                    Message::set('新闻分类添加成功');
                    Request::current()->redirect('/admin/newstype/add/');
                }
            }
        }
        $content = View::factory('admin/news/newstype_edit', $content_data)->set('data', $newstype)->render();
        $this->template = View::factory('admin/template')->set('content', $content);
    }

}
